//
//  MJLPlayListHelper.m
//  JZMCFM
//
//  Created by MJL on 15/11/3.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "MJLPlayListHelper.h"
#import "PlayListModel.h"
#import "PlayZTModel.h"
static MJLPlayListHelper *share = nil;

@interface MJLPlayListHelper()

@property(nonatomic,strong)NSMutableArray *listArray;
@property(nonatomic,strong)NSMutableArray *ztArray;
@end


@implementation MJLPlayListHelper

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[MJLPlayListHelper alloc] init];
    });
    return share;
}


#pragma mark 解析播放列表数据
- (void)downLoadPlayListDatawithString:(NSString *)string WithBlock:(Block)resultBlock
{
    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        
        NSURL *url = [NSURL URLWithString:string];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *tracksDict = dict[@"tracks"];
            NSArray *listArray = tracksDict[@"list"];
            [self.listArray removeAllObjects];
            for (NSDictionary *dict in listArray) {
                PlayListModel *model = [[PlayListModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.listArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                resultBlock();
            });
        }];
        
        
    });
    
    
}

#pragma mark 解析专题数据
- (void)downLoadZTDataWithString:(NSString *)string withBlock:(Block)result
{
    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSDictionary *albumInfoDict = responseObject[@"albumInfo"];
            PlayZTModel *model = [[PlayZTModel alloc]init];
            [model setValuesForKeysWithDictionary:albumInfoDict];
            dispatch_sync(dispatch_get_main_queue(), ^{
                result();
            });
            NSLog(@"%@",self.ztArray);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
        
    });
}



- (NSMutableArray *)listArray
{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (NSArray *)playListArray
{
    return [self.listArray copy];
}


- (NSMutableArray *)ztArray
{
    if (!_ztArray) {
        _ztArray = [NSMutableArray array];
    }
    return _ztArray;
}

@end
